var emails = eval('('+document.cookie.split(";")[0].split("=")[1]+')');
$(function () {
    var groupName = emails.groupName;
    var id = emails.id;
    $.ajax({
        type: 'get',
        url: "/user/list/all",
        //contentType: 'application/json;charset=utf-8',
        success: function(data){    //请求成功时的回调函数
            if(data.code == "1") {
                var userList = data.data;
                $.ajax({
                    type: 'get',
                    url: "/email-group/" + id,
                    //contentType: 'application/json;charset=utf-8',
                    success: function (data) {    //请求成功时的回调函数
                        if (data.code == "1") {
                            var userChecks = data.data.members;
                            for(var i = 0;i<userList.length;i++){
                                var flog = 0;
                                var user = userList[i];
                                //行
                                var str = $("<tr></tr>");
                                //列
                                var std = $("<th scope=\"row\"></th>");
                                var std1 = $("<td><div></div></td>");
                                var std2 = $("<td > <div><input type=\"checkbox\" name=\"items\" value='"+user.id+"'></div></td>");
                                for(var j = 0;j<userChecks.length;j++){
                                    if(userList[i].id==userChecks[j].id){
                                         flog = 1;
                                         break;
                                    }
                                }
                                if(flog){
                                    continue;
                                }
                                str.append(std2.clone());
                                str.append(std1.clone().text(user.name));
                                str.append(std1.clone().text(user.email));
                                str.append(std1.clone().text(user.createTime));
                                str.append(std1.clone().text(groupName));
                                str.appendTo($("#tabletext"));
                            }

                        } else {
                            alert(data.msg + data.data);
                        }
                    },
                });
            }else{
                alert(data.msg + data.data);
            }
        },
    });

});
function tianjia() {
    var fun = [];
    $('[name=items]:checkbox:checked').each(function(){
        var user={};
        var emailGroupInfo={};
        user.id = $(this).val();
        emailGroupInfo.id = emails.id;
        var relation = {};
        relation.user = user;
        relation.emailGroupInfo = emailGroupInfo;
        fun.push(relation);
    });
    $.ajax({
        type: 'post',
        url: "/email-group/add-members",
        contentType: 'application/json;charset=utf-8',
        data:JSON.stringify(fun),
        success: function(data){    //请求成功时的回调函数
            if(data.code == "1"){
                alert("添加成功");
                window.location.href="/user/emailDetails";
            }else{
                alert(data.msg + data.data);
            }
        },
    });
}
function goBack() {
    window.history.back()
}
